package qldc.pm.tienan.service.persistence;

import com.liferay.portal.kernel.cache.CacheRegistryUtil;
import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
import com.liferay.portal.kernel.dao.orm.FinderPath;
import com.liferay.portal.kernel.dao.orm.Query;
import com.liferay.portal.kernel.dao.orm.QueryPos;
import com.liferay.portal.kernel.dao.orm.QueryUtil;
import com.liferay.portal.kernel.dao.orm.Session;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.util.GetterUtil;
import com.liferay.portal.kernel.util.InstanceFactory;
import com.liferay.portal.kernel.util.OrderByComparator;
import com.liferay.portal.kernel.util.PropsKeys;
import com.liferay.portal.kernel.util.PropsUtil;
import com.liferay.portal.kernel.util.StringBundler;
import com.liferay.portal.kernel.util.StringPool;
import com.liferay.portal.kernel.util.StringUtil;
import com.liferay.portal.kernel.util.UnmodifiableList;
import com.liferay.portal.model.CacheModel;
import com.liferay.portal.model.ModelListener;
import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;

import qldc.pm.tienan.NoSuchKetQuaTrichLucException;
import qldc.pm.tienan.model.KetQuaTrichLuc;
import qldc.pm.tienan.model.impl.KetQuaTrichLucImpl;
import qldc.pm.tienan.model.impl.KetQuaTrichLucModelImpl;
import qldc.pm.tienan.service.persistence.KetQuaTrichLucPersistence;

import java.io.Serializable;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * The persistence implementation for the ket qua trich luc service.
 *
 * <p>
 * Caching information and settings can be found in <code>portal.properties</code>
 * </p>
 *
 * @author Brian Wing Shun Chan
 * @see KetQuaTrichLucPersistence
 * @see KetQuaTrichLucUtil
 * @generated
 */
public class KetQuaTrichLucPersistenceImpl extends BasePersistenceImpl<KetQuaTrichLuc>
    implements KetQuaTrichLucPersistence {
    /*
     * NOTE FOR DEVELOPERS:
     *
     * Never modify or reference this class directly. Always use {@link KetQuaTrichLucUtil} to access the ket qua trich luc persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
     */
    public static final String FINDER_CLASS_NAME_ENTITY = KetQuaTrichLucImpl.class.getName();
    public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List1";
    public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
        ".List2";
    public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
            KetQuaTrichLucModelImpl.FINDER_CACHE_ENABLED,
            KetQuaTrichLucImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
            "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
            KetQuaTrichLucModelImpl.FINDER_CACHE_ENABLED,
            KetQuaTrichLucImpl.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
    public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
            KetQuaTrichLucModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
    public static final FinderPath FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID = new FinderPath(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
            KetQuaTrichLucModelImpl.FINDER_CACHE_ENABLED,
            KetQuaTrichLucImpl.class, FINDER_CLASS_NAME_ENTITY,
            "fetchByYeuCauTrichLucId", new String[] { Long.class.getName() },
            KetQuaTrichLucModelImpl.YEUCAUTRICHLUCID_COLUMN_BITMASK);
    public static final FinderPath FINDER_PATH_COUNT_BY_YEUCAUTRICHLUCID = new FinderPath(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
            KetQuaTrichLucModelImpl.FINDER_CACHE_ENABLED, Long.class,
            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
            "countByYeuCauTrichLucId", new String[] { Long.class.getName() });
    private static final String _FINDER_COLUMN_YEUCAUTRICHLUCID_YEUCAUTRICHLUCID_2 =
        "ketQuaTrichLuc.yeuCauTrichLucId = ?";
    private static final String _SQL_SELECT_KETQUATRICHLUC = "SELECT ketQuaTrichLuc FROM KetQuaTrichLuc ketQuaTrichLuc";
    private static final String _SQL_SELECT_KETQUATRICHLUC_WHERE = "SELECT ketQuaTrichLuc FROM KetQuaTrichLuc ketQuaTrichLuc WHERE ";
    private static final String _SQL_COUNT_KETQUATRICHLUC = "SELECT COUNT(ketQuaTrichLuc) FROM KetQuaTrichLuc ketQuaTrichLuc";
    private static final String _SQL_COUNT_KETQUATRICHLUC_WHERE = "SELECT COUNT(ketQuaTrichLuc) FROM KetQuaTrichLuc ketQuaTrichLuc WHERE ";
    private static final String _ORDER_BY_ENTITY_ALIAS = "ketQuaTrichLuc.";
    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No KetQuaTrichLuc exists with the primary key ";
    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No KetQuaTrichLuc exists with the key {";
    private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = GetterUtil.getBoolean(PropsUtil.get(
                PropsKeys.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE));
    private static Log _log = LogFactoryUtil.getLog(KetQuaTrichLucPersistenceImpl.class);
    private static KetQuaTrichLuc _nullKetQuaTrichLuc = new KetQuaTrichLucImpl() {
            @Override
            public Object clone() {
                return this;
            }

            @Override
            public CacheModel<KetQuaTrichLuc> toCacheModel() {
                return _nullKetQuaTrichLucCacheModel;
            }
        };

    private static CacheModel<KetQuaTrichLuc> _nullKetQuaTrichLucCacheModel = new CacheModel<KetQuaTrichLuc>() {
            @Override
            public KetQuaTrichLuc toEntityModel() {
                return _nullKetQuaTrichLuc;
            }
        };

    public KetQuaTrichLucPersistenceImpl() {
        setModelClass(KetQuaTrichLuc.class);
    }

    /**
     * Returns the ket qua trich luc where yeuCauTrichLucId = &#63; or throws a {@link qldc.pm.tienan.NoSuchKetQuaTrichLucException} if it could not be found.
     *
     * @param yeuCauTrichLucId the yeu cau trich luc ID
     * @return the matching ket qua trich luc
     * @throws qldc.pm.tienan.NoSuchKetQuaTrichLucException if a matching ket qua trich luc could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public KetQuaTrichLuc findByYeuCauTrichLucId(long yeuCauTrichLucId)
        throws NoSuchKetQuaTrichLucException, SystemException {
        KetQuaTrichLuc ketQuaTrichLuc = fetchByYeuCauTrichLucId(yeuCauTrichLucId);

        if (ketQuaTrichLuc == null) {
            StringBundler msg = new StringBundler(4);

            msg.append(_NO_SUCH_ENTITY_WITH_KEY);

            msg.append("yeuCauTrichLucId=");
            msg.append(yeuCauTrichLucId);

            msg.append(StringPool.CLOSE_CURLY_BRACE);

            if (_log.isWarnEnabled()) {
                _log.warn(msg.toString());
            }

            throw new NoSuchKetQuaTrichLucException(msg.toString());
        }

        return ketQuaTrichLuc;
    }

    /**
     * Returns the ket qua trich luc where yeuCauTrichLucId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
     *
     * @param yeuCauTrichLucId the yeu cau trich luc ID
     * @return the matching ket qua trich luc, or <code>null</code> if a matching ket qua trich luc could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public KetQuaTrichLuc fetchByYeuCauTrichLucId(long yeuCauTrichLucId)
        throws SystemException {
        return fetchByYeuCauTrichLucId(yeuCauTrichLucId, true);
    }

    /**
     * Returns the ket qua trich luc where yeuCauTrichLucId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
     *
     * @param yeuCauTrichLucId the yeu cau trich luc ID
     * @param retrieveFromCache whether to use the finder cache
     * @return the matching ket qua trich luc, or <code>null</code> if a matching ket qua trich luc could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public KetQuaTrichLuc fetchByYeuCauTrichLucId(long yeuCauTrichLucId,
        boolean retrieveFromCache) throws SystemException {
        Object[] finderArgs = new Object[] { yeuCauTrichLucId };

        Object result = null;

        if (retrieveFromCache) {
            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID,
                    finderArgs, this);
        }

        if (result instanceof KetQuaTrichLuc) {
            KetQuaTrichLuc ketQuaTrichLuc = (KetQuaTrichLuc) result;

            if ((yeuCauTrichLucId != ketQuaTrichLuc.getYeuCauTrichLucId())) {
                result = null;
            }
        }

        if (result == null) {
            StringBundler query = new StringBundler(3);

            query.append(_SQL_SELECT_KETQUATRICHLUC_WHERE);

            query.append(_FINDER_COLUMN_YEUCAUTRICHLUCID_YEUCAUTRICHLUCID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(yeuCauTrichLucId);

                List<KetQuaTrichLuc> list = q.list();

                if (list.isEmpty()) {
                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID,
                        finderArgs, list);
                } else {
                    if ((list.size() > 1) && _log.isWarnEnabled()) {
                        _log.warn(
                            "KetQuaTrichLucPersistenceImpl.fetchByYeuCauTrichLucId(long, boolean) with parameters (" +
                            StringUtil.merge(finderArgs) +
                            ") yields a result set with more than 1 result. This violates the logical unique restriction. There is no order guarantee on which result is returned by this finder.");
                    }

                    KetQuaTrichLuc ketQuaTrichLuc = list.get(0);

                    result = ketQuaTrichLuc;

                    cacheResult(ketQuaTrichLuc);

                    if ((ketQuaTrichLuc.getYeuCauTrichLucId() != yeuCauTrichLucId)) {
                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID,
                            finderArgs, ketQuaTrichLuc);
                    }
                }
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID,
                    finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        if (result instanceof List<?>) {
            return null;
        } else {
            return (KetQuaTrichLuc) result;
        }
    }

    /**
     * Removes the ket qua trich luc where yeuCauTrichLucId = &#63; from the database.
     *
     * @param yeuCauTrichLucId the yeu cau trich luc ID
     * @return the ket qua trich luc that was removed
     * @throws SystemException if a system exception occurred
     */
    @Override
    public KetQuaTrichLuc removeByYeuCauTrichLucId(long yeuCauTrichLucId)
        throws NoSuchKetQuaTrichLucException, SystemException {
        KetQuaTrichLuc ketQuaTrichLuc = findByYeuCauTrichLucId(yeuCauTrichLucId);

        return remove(ketQuaTrichLuc);
    }

    /**
     * Returns the number of ket qua trich lucs where yeuCauTrichLucId = &#63;.
     *
     * @param yeuCauTrichLucId the yeu cau trich luc ID
     * @return the number of matching ket qua trich lucs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countByYeuCauTrichLucId(long yeuCauTrichLucId)
        throws SystemException {
        FinderPath finderPath = FINDER_PATH_COUNT_BY_YEUCAUTRICHLUCID;

        Object[] finderArgs = new Object[] { yeuCauTrichLucId };

        Long count = (Long) FinderCacheUtil.getResult(finderPath, finderArgs,
                this);

        if (count == null) {
            StringBundler query = new StringBundler(2);

            query.append(_SQL_COUNT_KETQUATRICHLUC_WHERE);

            query.append(_FINDER_COLUMN_YEUCAUTRICHLUCID_YEUCAUTRICHLUCID_2);

            String sql = query.toString();

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                QueryPos qPos = QueryPos.getInstance(q);

                qPos.add(yeuCauTrichLucId);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(finderPath, finderArgs, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Caches the ket qua trich luc in the entity cache if it is enabled.
     *
     * @param ketQuaTrichLuc the ket qua trich luc
     */
    @Override
    public void cacheResult(KetQuaTrichLuc ketQuaTrichLuc) {
        EntityCacheUtil.putResult(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
            KetQuaTrichLucImpl.class, ketQuaTrichLuc.getPrimaryKey(),
            ketQuaTrichLuc);

        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID,
            new Object[] { ketQuaTrichLuc.getYeuCauTrichLucId() },
            ketQuaTrichLuc);

        ketQuaTrichLuc.resetOriginalValues();
    }

    /**
     * Caches the ket qua trich lucs in the entity cache if it is enabled.
     *
     * @param ketQuaTrichLucs the ket qua trich lucs
     */
    @Override
    public void cacheResult(List<KetQuaTrichLuc> ketQuaTrichLucs) {
        for (KetQuaTrichLuc ketQuaTrichLuc : ketQuaTrichLucs) {
            if (EntityCacheUtil.getResult(
                        KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
                        KetQuaTrichLucImpl.class, ketQuaTrichLuc.getPrimaryKey()) == null) {
                cacheResult(ketQuaTrichLuc);
            } else {
                ketQuaTrichLuc.resetOriginalValues();
            }
        }
    }

    /**
     * Clears the cache for all ket qua trich lucs.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache() {
        if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
            CacheRegistryUtil.clear(KetQuaTrichLucImpl.class.getName());
        }

        EntityCacheUtil.clearCache(KetQuaTrichLucImpl.class.getName());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }

    /**
     * Clears the cache for the ket qua trich luc.
     *
     * <p>
     * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
     * </p>
     */
    @Override
    public void clearCache(KetQuaTrichLuc ketQuaTrichLuc) {
        EntityCacheUtil.removeResult(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
            KetQuaTrichLucImpl.class, ketQuaTrichLuc.getPrimaryKey());

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        clearUniqueFindersCache(ketQuaTrichLuc);
    }

    @Override
    public void clearCache(List<KetQuaTrichLuc> ketQuaTrichLucs) {
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);

        for (KetQuaTrichLuc ketQuaTrichLuc : ketQuaTrichLucs) {
            EntityCacheUtil.removeResult(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
                KetQuaTrichLucImpl.class, ketQuaTrichLuc.getPrimaryKey());

            clearUniqueFindersCache(ketQuaTrichLuc);
        }
    }

    protected void cacheUniqueFindersCache(KetQuaTrichLuc ketQuaTrichLuc) {
        if (ketQuaTrichLuc.isNew()) {
            Object[] args = new Object[] { ketQuaTrichLuc.getYeuCauTrichLucId() };

            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_YEUCAUTRICHLUCID,
                args, Long.valueOf(1));
            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID,
                args, ketQuaTrichLuc);
        } else {
            KetQuaTrichLucModelImpl ketQuaTrichLucModelImpl = (KetQuaTrichLucModelImpl) ketQuaTrichLuc;

            if ((ketQuaTrichLucModelImpl.getColumnBitmask() &
                    FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID.getColumnBitmask()) != 0) {
                Object[] args = new Object[] {
                        ketQuaTrichLuc.getYeuCauTrichLucId()
                    };

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_YEUCAUTRICHLUCID,
                    args, Long.valueOf(1));
                FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID,
                    args, ketQuaTrichLuc);
            }
        }
    }

    protected void clearUniqueFindersCache(KetQuaTrichLuc ketQuaTrichLuc) {
        KetQuaTrichLucModelImpl ketQuaTrichLucModelImpl = (KetQuaTrichLucModelImpl) ketQuaTrichLuc;

        Object[] args = new Object[] { ketQuaTrichLuc.getYeuCauTrichLucId() };

        FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_YEUCAUTRICHLUCID, args);
        FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID, args);

        if ((ketQuaTrichLucModelImpl.getColumnBitmask() &
                FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID.getColumnBitmask()) != 0) {
            args = new Object[] {
                    ketQuaTrichLucModelImpl.getOriginalYeuCauTrichLucId()
                };

            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_YEUCAUTRICHLUCID,
                args);
            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_YEUCAUTRICHLUCID,
                args);
        }
    }

    /**
     * Creates a new ket qua trich luc with the primary key. Does not add the ket qua trich luc to the database.
     *
     * @param ketQuaTrichLucId the primary key for the new ket qua trich luc
     * @return the new ket qua trich luc
     */
    @Override
    public KetQuaTrichLuc create(long ketQuaTrichLucId) {
        KetQuaTrichLuc ketQuaTrichLuc = new KetQuaTrichLucImpl();

        ketQuaTrichLuc.setNew(true);
        ketQuaTrichLuc.setPrimaryKey(ketQuaTrichLucId);

        return ketQuaTrichLuc;
    }

    /**
     * Removes the ket qua trich luc with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param ketQuaTrichLucId the primary key of the ket qua trich luc
     * @return the ket qua trich luc that was removed
     * @throws qldc.pm.tienan.NoSuchKetQuaTrichLucException if a ket qua trich luc with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public KetQuaTrichLuc remove(long ketQuaTrichLucId)
        throws NoSuchKetQuaTrichLucException, SystemException {
        return remove((Serializable) ketQuaTrichLucId);
    }

    /**
     * Removes the ket qua trich luc with the primary key from the database. Also notifies the appropriate model listeners.
     *
     * @param primaryKey the primary key of the ket qua trich luc
     * @return the ket qua trich luc that was removed
     * @throws qldc.pm.tienan.NoSuchKetQuaTrichLucException if a ket qua trich luc with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public KetQuaTrichLuc remove(Serializable primaryKey)
        throws NoSuchKetQuaTrichLucException, SystemException {
        Session session = null;

        try {
            session = openSession();

            KetQuaTrichLuc ketQuaTrichLuc = (KetQuaTrichLuc) session.get(KetQuaTrichLucImpl.class,
                    primaryKey);

            if (ketQuaTrichLuc == null) {
                if (_log.isWarnEnabled()) {
                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
                }

                throw new NoSuchKetQuaTrichLucException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                    primaryKey);
            }

            return remove(ketQuaTrichLuc);
        } catch (NoSuchKetQuaTrichLucException nsee) {
            throw nsee;
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }
    }

    @Override
    protected KetQuaTrichLuc removeImpl(KetQuaTrichLuc ketQuaTrichLuc)
        throws SystemException {
        ketQuaTrichLuc = toUnwrappedModel(ketQuaTrichLuc);

        Session session = null;

        try {
            session = openSession();

            if (!session.contains(ketQuaTrichLuc)) {
                ketQuaTrichLuc = (KetQuaTrichLuc) session.get(KetQuaTrichLucImpl.class,
                        ketQuaTrichLuc.getPrimaryKeyObj());
            }

            if (ketQuaTrichLuc != null) {
                session.delete(ketQuaTrichLuc);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        if (ketQuaTrichLuc != null) {
            clearCache(ketQuaTrichLuc);
        }

        return ketQuaTrichLuc;
    }

    @Override
    public KetQuaTrichLuc updateImpl(
        qldc.pm.tienan.model.KetQuaTrichLuc ketQuaTrichLuc)
        throws SystemException {
        ketQuaTrichLuc = toUnwrappedModel(ketQuaTrichLuc);

        boolean isNew = ketQuaTrichLuc.isNew();

        Session session = null;

        try {
            session = openSession();

            if (ketQuaTrichLuc.isNew()) {
                session.save(ketQuaTrichLuc);

                ketQuaTrichLuc.setNew(false);
            } else {
                session.merge(ketQuaTrichLuc);
            }
        } catch (Exception e) {
            throw processException(e);
        } finally {
            closeSession(session);
        }

        FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);

        if (isNew || !KetQuaTrichLucModelImpl.COLUMN_BITMASK_ENABLED) {
            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
        }

        EntityCacheUtil.putResult(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
            KetQuaTrichLucImpl.class, ketQuaTrichLuc.getPrimaryKey(),
            ketQuaTrichLuc);

        clearUniqueFindersCache(ketQuaTrichLuc);
        cacheUniqueFindersCache(ketQuaTrichLuc);

        return ketQuaTrichLuc;
    }

    protected KetQuaTrichLuc toUnwrappedModel(KetQuaTrichLuc ketQuaTrichLuc) {
        if (ketQuaTrichLuc instanceof KetQuaTrichLucImpl) {
            return ketQuaTrichLuc;
        }

        KetQuaTrichLucImpl ketQuaTrichLucImpl = new KetQuaTrichLucImpl();

        ketQuaTrichLucImpl.setNew(ketQuaTrichLuc.isNew());
        ketQuaTrichLucImpl.setPrimaryKey(ketQuaTrichLuc.getPrimaryKey());

        ketQuaTrichLucImpl.setKetQuaTrichLucId(ketQuaTrichLuc.getKetQuaTrichLucId());
        ketQuaTrichLucImpl.setYeuCauTrichLucId(ketQuaTrichLuc.getYeuCauTrichLucId());
        ketQuaTrichLucImpl.setMaKetQuaTrichLuc(ketQuaTrichLuc.getMaKetQuaTrichLuc());
        ketQuaTrichLucImpl.setKinhGui(ketQuaTrichLuc.getKinhGui());
        ketQuaTrichLucImpl.setTinhTrangAnTich(ketQuaTrichLuc.getTinhTrangAnTich());
        ketQuaTrichLucImpl.setThongTinAnTich(ketQuaTrichLuc.getThongTinAnTich());
        ketQuaTrichLucImpl.setToiDanh(ketQuaTrichLuc.getToiDanh());
        ketQuaTrichLucImpl.setHinhPhatChinh(ketQuaTrichLuc.getHinhPhatChinh());
        ketQuaTrichLucImpl.setHinhPhatBoSung(ketQuaTrichLuc.getHinhPhatBoSung());
        ketQuaTrichLucImpl.setTrinhTrangThiHanhAn(ketQuaTrichLuc.getTrinhTrangThiHanhAn());
        ketQuaTrichLucImpl.setNguoiTraCuuHoSo(ketQuaTrichLuc.getNguoiTraCuuHoSo());
        ketQuaTrichLucImpl.setNgayTraCuu(ketQuaTrichLuc.getNgayTraCuu());
        ketQuaTrichLucImpl.setKetQua(ketQuaTrichLuc.getKetQua());

        return ketQuaTrichLucImpl;
    }

    /**
     * Returns the ket qua trich luc with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
     *
     * @param primaryKey the primary key of the ket qua trich luc
     * @return the ket qua trich luc
     * @throws qldc.pm.tienan.NoSuchKetQuaTrichLucException if a ket qua trich luc with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public KetQuaTrichLuc findByPrimaryKey(Serializable primaryKey)
        throws NoSuchKetQuaTrichLucException, SystemException {
        KetQuaTrichLuc ketQuaTrichLuc = fetchByPrimaryKey(primaryKey);

        if (ketQuaTrichLuc == null) {
            if (_log.isWarnEnabled()) {
                _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
            }

            throw new NoSuchKetQuaTrichLucException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
                primaryKey);
        }

        return ketQuaTrichLuc;
    }

    /**
     * Returns the ket qua trich luc with the primary key or throws a {@link qldc.pm.tienan.NoSuchKetQuaTrichLucException} if it could not be found.
     *
     * @param ketQuaTrichLucId the primary key of the ket qua trich luc
     * @return the ket qua trich luc
     * @throws qldc.pm.tienan.NoSuchKetQuaTrichLucException if a ket qua trich luc with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public KetQuaTrichLuc findByPrimaryKey(long ketQuaTrichLucId)
        throws NoSuchKetQuaTrichLucException, SystemException {
        return findByPrimaryKey((Serializable) ketQuaTrichLucId);
    }

    /**
     * Returns the ket qua trich luc with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param primaryKey the primary key of the ket qua trich luc
     * @return the ket qua trich luc, or <code>null</code> if a ket qua trich luc with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public KetQuaTrichLuc fetchByPrimaryKey(Serializable primaryKey)
        throws SystemException {
        KetQuaTrichLuc ketQuaTrichLuc = (KetQuaTrichLuc) EntityCacheUtil.getResult(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
                KetQuaTrichLucImpl.class, primaryKey);

        if (ketQuaTrichLuc == _nullKetQuaTrichLuc) {
            return null;
        }

        if (ketQuaTrichLuc == null) {
            Session session = null;

            try {
                session = openSession();

                ketQuaTrichLuc = (KetQuaTrichLuc) session.get(KetQuaTrichLucImpl.class,
                        primaryKey);

                if (ketQuaTrichLuc != null) {
                    cacheResult(ketQuaTrichLuc);
                } else {
                    EntityCacheUtil.putResult(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
                        KetQuaTrichLucImpl.class, primaryKey,
                        _nullKetQuaTrichLuc);
                }
            } catch (Exception e) {
                EntityCacheUtil.removeResult(KetQuaTrichLucModelImpl.ENTITY_CACHE_ENABLED,
                    KetQuaTrichLucImpl.class, primaryKey);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return ketQuaTrichLuc;
    }

    /**
     * Returns the ket qua trich luc with the primary key or returns <code>null</code> if it could not be found.
     *
     * @param ketQuaTrichLucId the primary key of the ket qua trich luc
     * @return the ket qua trich luc, or <code>null</code> if a ket qua trich luc with the primary key could not be found
     * @throws SystemException if a system exception occurred
     */
    @Override
    public KetQuaTrichLuc fetchByPrimaryKey(long ketQuaTrichLucId)
        throws SystemException {
        return fetchByPrimaryKey((Serializable) ketQuaTrichLucId);
    }

    /**
     * Returns all the ket qua trich lucs.
     *
     * @return the ket qua trich lucs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<KetQuaTrichLuc> findAll() throws SystemException {
        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
    }

    /**
     * Returns a range of all the ket qua trich lucs.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.KetQuaTrichLucModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of ket qua trich lucs
     * @param end the upper bound of the range of ket qua trich lucs (not inclusive)
     * @return the range of ket qua trich lucs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<KetQuaTrichLuc> findAll(int start, int end)
        throws SystemException {
        return findAll(start, end, null);
    }

    /**
     * Returns an ordered range of all the ket qua trich lucs.
     *
     * <p>
     * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link qldc.pm.tienan.model.impl.KetQuaTrichLucModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
     * </p>
     *
     * @param start the lower bound of the range of ket qua trich lucs
     * @param end the upper bound of the range of ket qua trich lucs (not inclusive)
     * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
     * @return the ordered range of ket qua trich lucs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public List<KetQuaTrichLuc> findAll(int start, int end,
        OrderByComparator orderByComparator) throws SystemException {
        boolean pagination = true;
        FinderPath finderPath = null;
        Object[] finderArgs = null;

        if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
                (orderByComparator == null)) {
            pagination = false;
            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
            finderArgs = FINDER_ARGS_EMPTY;
        } else {
            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
            finderArgs = new Object[] { start, end, orderByComparator };
        }

        List<KetQuaTrichLuc> list = (List<KetQuaTrichLuc>) FinderCacheUtil.getResult(finderPath,
                finderArgs, this);

        if (list == null) {
            StringBundler query = null;
            String sql = null;

            if (orderByComparator != null) {
                query = new StringBundler(2 +
                        (orderByComparator.getOrderByFields().length * 3));

                query.append(_SQL_SELECT_KETQUATRICHLUC);

                appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
                    orderByComparator);

                sql = query.toString();
            } else {
                sql = _SQL_SELECT_KETQUATRICHLUC;

                if (pagination) {
                    sql = sql.concat(KetQuaTrichLucModelImpl.ORDER_BY_JPQL);
                }
            }

            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(sql);

                if (!pagination) {
                    list = (List<KetQuaTrichLuc>) QueryUtil.list(q,
                            getDialect(), start, end, false);

                    Collections.sort(list);

                    list = new UnmodifiableList<KetQuaTrichLuc>(list);
                } else {
                    list = (List<KetQuaTrichLuc>) QueryUtil.list(q,
                            getDialect(), start, end);
                }

                cacheResult(list);

                FinderCacheUtil.putResult(finderPath, finderArgs, list);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(finderPath, finderArgs);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return list;
    }

    /**
     * Removes all the ket qua trich lucs from the database.
     *
     * @throws SystemException if a system exception occurred
     */
    @Override
    public void removeAll() throws SystemException {
        for (KetQuaTrichLuc ketQuaTrichLuc : findAll()) {
            remove(ketQuaTrichLuc);
        }
    }

    /**
     * Returns the number of ket qua trich lucs.
     *
     * @return the number of ket qua trich lucs
     * @throws SystemException if a system exception occurred
     */
    @Override
    public int countAll() throws SystemException {
        Long count = (Long) FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
                FINDER_ARGS_EMPTY, this);

        if (count == null) {
            Session session = null;

            try {
                session = openSession();

                Query q = session.createQuery(_SQL_COUNT_KETQUATRICHLUC);

                count = (Long) q.uniqueResult();

                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY, count);
            } catch (Exception e) {
                FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
                    FINDER_ARGS_EMPTY);

                throw processException(e);
            } finally {
                closeSession(session);
            }
        }

        return count.intValue();
    }

    /**
     * Initializes the ket qua trich luc persistence.
     */
    public void afterPropertiesSet() {
        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
                    com.liferay.util.service.ServiceProps.get(
                        "value.object.listener.qldc.pm.tienan.model.KetQuaTrichLuc")));

        if (listenerClassNames.length > 0) {
            try {
                List<ModelListener<KetQuaTrichLuc>> listenersList = new ArrayList<ModelListener<KetQuaTrichLuc>>();

                for (String listenerClassName : listenerClassNames) {
                    listenersList.add((ModelListener<KetQuaTrichLuc>) InstanceFactory.newInstance(
                            getClassLoader(), listenerClassName));
                }

                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
            } catch (Exception e) {
                _log.error(e);
            }
        }
    }

    public void destroy() {
        EntityCacheUtil.removeCache(KetQuaTrichLucImpl.class.getName());
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
        FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
    }
}
